package org.hackystat.devcathlon.ui.page.commons.panels.clock;

import java.util.TimeZone;

import org.apache.wicket.ajax.AbstractAjaxTimerBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.util.time.Duration;

/**
 * Creates a dynamic clock that updates every second.
 * @author Anthony Du
 *
 */
public class ClockPanel extends Panel {
  /** Support for serialization. */
  private static final long serialVersionUID = 1L;
  
  /**
   * Clock panel constructor.
   * @param id The panel id
   */
  public ClockPanel(String id) {
    super(id);
    // create clock components for different timezones
    final Clock hi = new Clock("hi", TimeZone.getTimeZone("GMT-10"));
    // make components print out id attrs so they can be updated via ajax
    hi.setOutputMarkupId(true);
    // add the components to the panel and add a markup id setter to
    // each component.
    add(hi);
    // add the timer behavior to the page and make it update all
    // other components as well
    add(new AbstractAjaxTimerBehavior(Duration.seconds(1))
    {
      /** Support for serialization. */
      private static final long serialVersionUID = 1L;

      /**
       * @see org.apache.wicket.ajax.AbstractAjaxTimerBehavior
       * #onTimer(org.apache.wicket.ajax.AjaxRequestTarget)
       */
      protected void onTimer(AjaxRequestTarget target) {
        target.addComponent(hi);
      }
    });
  }
}
